<?php namespace App\Repositories\Eloquent\User;

use App\Models\User;
use App\Models\UserInvite;
use App\Repositories\Eloquent\User\Entity\SocialiteUser;
use App\Repositories\User\UserRepositoryInterface;

class UserRepository implements UserRepositoryInterface
{
    /**
     * @var User
     */
    protected $model;

    /**
     * UserRepository constructor.
     *
     * @param User $user
     */
    public function __construct(User $user)
    {
        $this->model = $user;
    }

    /**
     * Find user or users by user id.
     *
     * @param $id
     *
     * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Model[]|null
     */
    public function show($id)
    {
        return User::find($id);
    }

    /**
     * Find user or users by uid.
     *
     * @param $id
     *
     * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Model[]|null
     */
    public function showByUid($uid)
    {
        return User::query()->where('uid', $uid)->first();
    }

    /**
     * 新建用户
     *
     * @param SocialiteUser $user
     *
     * @return \Illuminate\Database\Eloquent\Model
     */
    public function store(SocialiteUser $user)
    {
        $model = new User();
        $data['name'] = $user->nick_name;
        $data['device_type'] = $user->device_type;
        $data['avatar'] = $user->avatar ?: '';
        if ($data['avatar'] && !url()->isValidUrl($data['avatar'])) {
            $data['avatar'] = '';
        }
        $data['uid'] = $this->createUniqueUid();
        $model->fill(array_filter($data));
        $model->save();

        return $model;
    }

    /**
     * 修改用户信息
     *
     * @param               $user_id
     * @param SocialiteUser $user
     */
    public function update($user_id, SocialiteUser $user)
    {
        $model = $this->show($user_id);
        $model->name = $user->nick_name;
        if (url()->isValidUrl($user->avatar)) {
            $model->avatar = $user->avatar;
        }

        return $model->save();
    }

    /**
     * 创建用户唯一六位数字的uid
     *
     * @param int $i
     *
     * @return number|string
     */
    private function createUniqueUid($i = 0)
    {
        if ($i >= 5) {
            return 0;
        }
        $random_str = substr(sha1(str_random(10)), 0, 5);
        $uid = hexdec($random_str);
        if (strlen($uid) < 6) {
            $uid = random_int(1, 9) * 100000 + $random_str;
        }
        $uid = (int)substr($uid, 0, 6);
        if (!$this->showByUid($uid)) {
            return $uid;
        }
        $i++;

        return $this->createUniqueUid($i);
    }

    /**
     * 增加邀请记录
     *
     * @param $user_id
     * @param $invite_uid
     *
     * @return bool
     */
    public function storeUserInvite($user_id, $invite_uid)
    {
        if ($this->getUserInvite($user_id)) {
            return false;
        }

        $model = new UserInvite();
        $model::unguard();
        $model->fill(compact('user_id', 'invite_uid'));

        return $model->save();
    }

    /**
     * 用户邀请人uid
     *
     * @param $user_id
     *
     * @return int|null
     */
    public function getUserInviteUid($user_id)
    {
        $invite = $this->getUserInvite($user_id);

        return $invite ? (int)$invite->invite_uid : null;
    }

    /**
     * 用户邀请记录
     *
     * @param $user_id
     *
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null|static|static[]
     */
    public function getUserInvite($user_id)
    {
        return UserInvite::query()->where('user_id', (int)$user_id)->first();
    }
}
